
import datetime
from datetime import timedelta
from django.shortcuts import render
from django.http import HttpResponse
from .data_method import *
from . import models
from django.contrib.auth.models import User 

# Create your views here.


def base(request):

    return render(request, 'scrum/base.html')

def index(request):

    product_type_id = request.GET.get("product_type_id")

    if product_type_id == None:

        product_type_id = 1


    context = {

        "product_type_id": product_type_id,

        "product_type_detail": ProductTypeDetail().get(product_type_id)["resultBody"],

        "tasks_in_progress_list": tasks_in_progress()["resultBody"] , # 任务统计

        "get_product_by_type": get_product_by_type(product_type_id, 2)["resultBody"],  # 项目列表

    }

    return render(request, "scrum/index.html", context=context)

def product(request):

    all_user = User.objects.all()

    all_product = models.Product.objects.all()

    context = {

        "all_user": all_user,

        "all_product": all_product

    }

    return render(request, "scrum/product.html", context=context)


def product_table(request):

    keyword = request.GET.get('keyword')
    product_type_id = request.GET.get('product_type_id')
    master_id = request.GET.get('master_id')
    operation = request.GET.get('operation')

    qurey = {}

    if bool(keyword):

        qurey["product__contains"] = keyword
    
    if product_type_id != '0':

        qurey["product_type__id"] = product_type_id

    if master_id != '0':

        qurey['master__id'] = master_id

    if operation != 'all':

        qurey["operation"] = operation

    all_product = models.Product.objects.filter(**qurey)

    result = ''

    tr = '''\
    <tr style="color:{}" onclick="showProductBacklogs({})"  title="{}" >
      <th scope="row">{}</th>
      <td>{}</td>
      <td>{}</td>
      <td>{}</td>
      <td>{}</td>
      <td>{}</td>
      <td>{}</td>

    </tr>
    '''
    for product in all_product:

        color = ''

        if product.operation in [1, 2]:

            if product.end_date.replace(tzinfo=None) < datetime.datetime.utcnow(): # 在进行的任务超时未完成

                color = 'red'

            elif product.end_date.replace(tzinfo=None) < datetime.datetime.utcnow() + datetime.timedelta(days=1): # 在进行的任务还省不到1天时间

                color = '#ff6347'

            elif product.end_date.replace(tzinfo=None) < datetime.datetime.utcnow() + datetime.timedelta(days=3): # 在进行的任务还省不到3天时间
                
                color = 'green'

        if product.operation == 0:

            operation = "撤销"

        elif product.operation == 1:
            operation = "等待"

        elif product.operation == 2:

            operation = "进行中"

        elif product.operation == 3:

            operation = "完成"

        result += tr.format(
            color,
            product.id,
            product.goal,
            product.id,
            product.product,
            product.product_code,
            product.master,
            product.start_date,
            product.end_date,
            operation
        )

    return HttpResponse(result)


def product_backlogs(request):

    product_id =  request.GET.get("product_id")

    if product_id == None:

        product_id = 1

    context = {
        "product_id": product_id,
        "product": get_product_by_id(product_id)['resultBody'],
        "get_product_backlog_by_product_id": get_product_backlog_by_product_id(product_id)['resultBody'],
    }
    return render(request, "scrum/product_backlogs.html", context=context)

    
def instructions(request):

    return render(request, "scrum/百纳信达SCRUM系统使用说明.html")

def explain(request):

    return render(request, "scrum/explain.html")

def week(request):

    user_id = request.GET.get('user_id')

    if not bool(user_id):

        user_id = 0

    try:
        user = User.objects.get(id=user_id)

    except User.DoesNotExist:

        user = None


    now = datetime.datetime.now()

    context = {}

    #上周第一天和最后一天
    last_week_start = now - timedelta(days=now.weekday()+7, hours=now.hour, minutes=now.minute)
    last_week_end = now - timedelta(days=now.weekday()+1, hours=now.hour, minutes=now.minute)

    #本周第一天和最后一天
    this_week_start = now - timedelta(days=now.weekday(), hours=now.hour, minutes=now.minute)
    this_week_end = now + timedelta(days=6-now.weekday(), hours=24-now.hour, minutes=60-now.minute)

    # 上周条目
    last_week_list = models.ProductBacklog.objects.filter(start_date__gte = last_week_start, start_date__lte = last_week_end, responsible__id=user_id)
    this_week_list = models.ProductBacklog.objects.filter(start_date__gte = this_week_start, start_date__lte = this_week_end, responsible__id=user_id)
    future_week_list = models.ProductBacklog.objects.filter(start_date__gte = this_week_end, responsible__id=user_id).order_by('start_date')

    all_user = User.objects.all()

    context = {
        "user": user,
        "last_week_list": last_week_list,
        "this_week_list": this_week_list,
        "future_week_list": future_week_list,
        "all_user": all_user,
    }

    return render(request, "scrum/week.html", context=context)

